package com.sevici;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sevici.model.Distrito;
import com.sevici.model.Estacion;

public class XMLHandlerDatosEstacion extends DefaultHandler{

	// ===========================================================
	// Campos
	// ===========================================================

	
	@SuppressWarnings("unused")
	private boolean in_station = false;
	@SuppressWarnings("unused")
	private boolean in_available = false;
		@SuppressWarnings("unused")
	private boolean in_free = false;
	@SuppressWarnings("unused")
	private boolean in_total = false;
	@SuppressWarnings("unused")
	private boolean in_ticket = false;
	
	private String station   = "";
	private String available = "";
	private String free = "";
	private String total = "";
	private String ticket = "";
	

	// ===========================================================
	// Metodos
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
	}

	@Override
	public void endDocument() throws SAXException {
		// No hacemos nada
	}

	/** Se ejecuta cuando encuentra tags como:
	 * <tag>
	 * Puede leer atributos cuando se encuentra con:
	 * </tag><tag attribute="attributeValue">*/
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		cierraTodos();
		if (localName.equals("station")) {
			this.in_station = true;
		}else if (localName.equals("available")) {
			this.in_available = true;			
		}else if (localName.equals("ticket")) {
			this.in_ticket = true;			
		}else if (localName.equals("total")) {
			this.in_total = true;			
		}else if (localName.equals("free")) {
			this.in_free = true;			
		}
	}
	private void cierraTodos() {		
		in_station = false;
		in_available = false;
		in_free = false;
		in_total = false;
		in_ticket = false;
	}

	/** Se ejecuta en tags de cierre:
	 * </tag> */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
	throws SAXException {
		if (localName.equals("station")) {
			this.in_station = false;
		}else if (localName.equals("available")) {
			this.in_available = false;			
		}else if (localName.equals("ticket")) {
			this.in_ticket = false;			
		}else if (localName.equals("total")) {
			this.in_total = false;			
		}else if (localName.equals("free")) {
			this.in_free = false;			
		}
	}
	
	/** Se ejecuta cuando se encuentra con la siguiente estructura:
	 * <tag>characters</tag> */
	@Override
	public void characters(char ch[], int start, int length) {
		String valor = new String(ch, start, length);
		if(this.in_station){
			setStation(valor);
		}
		if(this.in_available){
			setAvailable(valor);			
		}
		if(this.in_free){
			setFree(valor);
		}
		if(this.in_total){
			setTotal(valor);
		}
		if(this.in_ticket){
			setTicket(valor);
		}
	}

	public String getStation() {
		return station;
	}

	public void setStation(String station) {
		this.station = station;
	}

	public String getAvailable() {
		return available;
	}

	public void setAvailable(String available) {
		this.available = available;
	}

	public String getFree() {
		return free;
	}

	public void setFree(String free) {
		this.free = free;
	}

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public String getTicket() {
		return ticket;
	}

	public void setTicket(String ticket) {
		this.ticket = ticket;
	}
}
